from tortoise import BaseDBAsyncClient


async def upgrade(db: BaseDBAsyncClient) -> str:
    return """
        CREATE TABLE IF NOT EXISTS `xm_access` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `create_time` DATETIME(6) NOT NULL  COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6),
    `update_time` DATETIME(6) NOT NULL  COMMENT '更新时间' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    `access_name` VARCHAR(15) NOT NULL  COMMENT '权限名称',
    `parent_id` INT NOT NULL  COMMENT '父id' DEFAULT 0,
    `scopes` VARCHAR(255) NOT NULL UNIQUE COMMENT '权限范围标识',
    `access_desc` VARCHAR(255)   COMMENT '权限描述',
    `menu_icon` VARCHAR(255)   COMMENT '菜单图标',
    `is_check` BOOL NOT NULL  COMMENT '是否验证权限 True为验证 False不验证' DEFAULT 0,
    `is_menu` BOOL NOT NULL  COMMENT '是否为菜单 True菜单 False不是菜单' DEFAULT 0
) CHARACTER SET utf8mb4 COMMENT='权限表';
CREATE TABLE IF NOT EXISTS `xm_access_log` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `create_time` DATETIME(6) NOT NULL  COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6),
    `update_time` DATETIME(6) NOT NULL  COMMENT '更新时间' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    `admin_id` INT NOT NULL  COMMENT '用户ID',
    `target_url` VARCHAR(255)   COMMENT '访问的url',
    `user_agent` LONGTEXT   COMMENT '访问UA',
    `request_params` JSON   COMMENT '请求参数get|post',
    `ip` VARCHAR(32)   COMMENT '访问IP',
    `note` VARCHAR(255)   COMMENT '备注'
) CHARACTER SET utf8mb4 COMMENT='用户操作记录表';
CREATE TABLE IF NOT EXISTS `xm_ad` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `title` VARCHAR(255) NOT NULL,
    `sort` INT NOT NULL  DEFAULT 0,
    `status` BOOL NOT NULL  DEFAULT 1,
    `is_sys` BOOL NOT NULL  DEFAULT 0,
    `a_key` VARCHAR(20)
) CHARACTER SET utf8mb4 COMMENT='广告';
CREATE TABLE IF NOT EXISTS `xm_ad_picture` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `title` VARCHAR(255) NOT NULL,
    `pic` VARCHAR(255) NOT NULL,
    `link` VARCHAR(255),
    `sort` INT NOT NULL  DEFAULT 0,
    `status` BOOL NOT NULL  DEFAULT 1,
    `ad_id` INT,
    CONSTRAINT `fk_xm_ad_pi_xm_ad_1b137ad8` FOREIGN KEY (`ad_id`) REFERENCES `xm_ad` (`id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4 COMMENT='广告图片';
CREATE TABLE IF NOT EXISTS `xm_admin` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `create_time` DATETIME(6) NOT NULL  COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6),
    `update_time` DATETIME(6) NOT NULL  COMMENT '更新时间' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    `adminname` VARCHAR(20)   COMMENT '用户名',
    `admin_type` BOOL NOT NULL  COMMENT '用户类型 True:超级管理员 False:普通管理员' DEFAULT 0,
    `password` VARCHAR(255),
    `nickname` VARCHAR(255) NOT NULL  COMMENT '昵称' DEFAULT 'xiaomi',
    `admin_phone` VARCHAR(11)   COMMENT '手机号',
    `admin_email` VARCHAR(255)   COMMENT '邮箱',
    `full_name` VARCHAR(255)   COMMENT '姓名',
    `admin_status` INT NOT NULL  COMMENT '0未激活 1正常 2禁用' DEFAULT 0,
    `header_img` VARCHAR(255)   COMMENT '头像',
    `sex` INT   COMMENT '0未知 1男 2女' DEFAULT 0,
    `remarks` VARCHAR(30)   COMMENT '备注',
    `client_host` VARCHAR(19)   COMMENT '访问IP'
) CHARACTER SET utf8mb4 COMMENT='用户表';
CREATE TABLE IF NOT EXISTS `xm_category` (
    `cate_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `cate_name` VARCHAR(30) NOT NULL  COMMENT '栏目名称',
    `cate_type` SMALLINT NOT NULL  COMMENT '类型：LINK(-2), SINGLE_PAGE(-1), ARTICLE(1), PRODUCT(2), JOB(3)' DEFAULT 1,
    `cate_url` VARCHAR(255)   COMMENT '链接地址',
    `sort` INT NOT NULL  COMMENT '排序' DEFAULT 0,
    `status` BOOL NOT NULL  COMMENT '状态' DEFAULT 0,
    `parent_id` INT,
    CONSTRAINT `fk_xm_categ_xm_categ_caf67705` FOREIGN KEY (`parent_id`) REFERENCES `xm_category` (`cate_id`) ON DELETE SET NULL,
    KEY `idx_xm_category_parent__f34639` (`parent_id`)
) CHARACTER SET utf8mb4 COMMENT='栏目表';
CREATE TABLE IF NOT EXISTS `xm_config_group` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `group_title` VARCHAR(50) NOT NULL  DEFAULT '',
    `sort` INT NOT NULL  DEFAULT 0,
    `group_key` VARCHAR(50) NOT NULL  DEFAULT '',
    `status` INT NOT NULL  DEFAULT 0,
    `types` INT NOT NULL  DEFAULT 0
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `xm_config` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `config_key` VARCHAR(50) NOT NULL  DEFAULT '',
    `config_title` VARCHAR(50) NOT NULL  DEFAULT '',
    `config_value` LONGTEXT NOT NULL,
    `sort` INT NOT NULL  DEFAULT 0,
    `config_type` INT NOT NULL  DEFAULT 0,
    `data_value` LONGTEXT NOT NULL,
    `data_text` VARCHAR(255),
    `right_type` INT NOT NULL  DEFAULT 0,
    `upload_type` INT NOT NULL  DEFAULT 0,
    `status` INT NOT NULL  DEFAULT 0,
    `is_sys` INT NOT NULL  DEFAULT 0,
    `is_hide` SMALLINT NOT NULL  DEFAULT 0,
    `group_id` INT,
    CONSTRAINT `fk_xm_confi_xm_confi_b225da2a` FOREIGN KEY (`group_id`) REFERENCES `xm_config_group` (`id`) ON DELETE CASCADE,
    KEY `idx_xm_config_config__cb8a29` (`config_key`, `group_id`)
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `xm_content` (
    `create_time` DATETIME(6) NOT NULL  COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6),
    `update_time` DATETIME(6) NOT NULL  COMMENT '更新时间' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `title` VARCHAR(255),
    `isnice` BOOL NOT NULL  DEFAULT 0,
    `istop` BOOL NOT NULL  DEFAULT 0,
    `islock` BOOL NOT NULL  DEFAULT 0,
    `category_id` INT,
    CONSTRAINT `fk_xm_conte_xm_categ_bca8b16b` FOREIGN KEY (`category_id`) REFERENCES `xm_category` (`cate_id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4 COMMENT='内容表';
CREATE TABLE IF NOT EXISTS `xm_model_job` (
    `jobid` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `content` LONGTEXT NOT NULL,
    `con_id` INT NOT NULL UNIQUE,
    CONSTRAINT `fk_xm_model_xm_conte_954196e4` FOREIGN KEY (`con_id`) REFERENCES `xm_content` (`id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4 COMMENT='招聘模型表';
CREATE TABLE IF NOT EXISTS `xm_model_news` (
    `newsid` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `content` LONGTEXT NOT NULL,
    `con_id` INT NOT NULL UNIQUE,
    CONSTRAINT `fk_xm_model_xm_conte_45750c48` FOREIGN KEY (`con_id`) REFERENCES `xm_content` (`id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4 COMMENT='新闻模型表';
CREATE TABLE IF NOT EXISTS `xm_model_page` (
    `pageid` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `content` LONGTEXT NOT NULL,
    `con_id` INT NOT NULL UNIQUE,
    CONSTRAINT `fk_xm_model_xm_categ_02039bb1` FOREIGN KEY (`con_id`) REFERENCES `xm_category` (`cate_id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4 COMMENT='单页模型表';
CREATE TABLE IF NOT EXISTS `xm_model_pro` (
    `proid` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `content` LONGTEXT NOT NULL,
    `con_id` INT NOT NULL UNIQUE,
    CONSTRAINT `fk_xm_model_xm_conte_9ee80f71` FOREIGN KEY (`con_id`) REFERENCES `xm_content` (`id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4 COMMENT='产品模型表';
CREATE TABLE IF NOT EXISTS `xm_picture_group` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `group_name` VARCHAR(255) NOT NULL,
    `status` BOOL NOT NULL  DEFAULT 1,
    `sort` INT NOT NULL  DEFAULT 0
) CHARACTER SET utf8mb4 COMMENT='图片分组表';
CREATE TABLE IF NOT EXISTS `xm_picture` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `title` VARCHAR(255) NOT NULL,
    `pic` VARCHAR(255) NOT NULL,
    `hits` INT NOT NULL  DEFAULT 0,
    `sort` INT NOT NULL  DEFAULT 0,
    `author` VARCHAR(255),
    `description` LONGTEXT,
    `is_nice` BOOL NOT NULL  DEFAULT 0,
    `is_top` BOOL NOT NULL  DEFAULT 0,
    `status` BOOL NOT NULL  DEFAULT 1,
    `create_time` DATETIME(6) NOT NULL  COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6),
    `update_time` DATETIME(6) NOT NULL  COMMENT '更新时间' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    `group_id` INT,
    CONSTRAINT `fk_xm_pictu_xm_pictu_09f7fd5b` FOREIGN KEY (`group_id`) REFERENCES `xm_picture_group` (`id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4 COMMENT='图片表';
CREATE TABLE IF NOT EXISTS `xm_role` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `create_time` DATETIME(6) NOT NULL  COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6),
    `update_time` DATETIME(6) NOT NULL  COMMENT '更新时间' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    `role_name` VARCHAR(15) NOT NULL  COMMENT '角色名称',
    `role_status` BOOL NOT NULL  COMMENT 'True:启用 False:禁用' DEFAULT 0,
    `role_desc` VARCHAR(255)   COMMENT '角色描述'
) CHARACTER SET utf8mb4 COMMENT='角色表';
CREATE TABLE IF NOT EXISTS `xm_tags` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `title` VARCHAR(255) NOT NULL,
    `hits` INT NOT NULL  DEFAULT 0,
    `status` BOOL NOT NULL  DEFAULT 1
) CHARACTER SET utf8mb4 COMMENT='标签表';
CREATE TABLE IF NOT EXISTS `aerich` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `version` VARCHAR(255) NOT NULL,
    `app` VARCHAR(100) NOT NULL,
    `content` JSON NOT NULL
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `xm_admin_role` (
    `xm_admin_id` INT NOT NULL,
    `role_id` INT NOT NULL,
    FOREIGN KEY (`xm_admin_id`) REFERENCES `xm_admin` (`id`) ON DELETE CASCADE,
    FOREIGN KEY (`role_id`) REFERENCES `xm_role` (`id`) ON DELETE CASCADE,
    UNIQUE KEY `uidx_xm_admin_ro_xm_admi_1faacb` (`xm_admin_id`, `role_id`)
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `xm_picture_tags` (
    `xm_picture_id` INT NOT NULL,
    `tags_id` INT NOT NULL,
    FOREIGN KEY (`xm_picture_id`) REFERENCES `xm_picture` (`id`) ON DELETE CASCADE,
    FOREIGN KEY (`tags_id`) REFERENCES `xm_tags` (`id`) ON DELETE CASCADE,
    UNIQUE KEY `uidx_xm_picture__xm_pict_8d99f5` (`xm_picture_id`, `tags_id`)
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `xm_role_access` (
    `xm_role_id` INT NOT NULL,
    `access_id` INT NOT NULL,
    FOREIGN KEY (`xm_role_id`) REFERENCES `xm_role` (`id`) ON DELETE CASCADE,
    FOREIGN KEY (`access_id`) REFERENCES `xm_access` (`id`) ON DELETE CASCADE,
    UNIQUE KEY `uidx_xm_role_acc_xm_role_400fe9` (`xm_role_id`, `access_id`)
) CHARACTER SET utf8mb4;"""


async def downgrade(db: BaseDBAsyncClient) -> str:
    return """
        """
